//
//  ViewController.m
//  VideoDemo
//
//  Created by C on 2019/8/19.
//  Copyright © 2019 c. All rights reserved.
//

#import "ViewController.h"
#import "WQVideoPreviewViewController.h"
#import "WQVideoPlayViewController.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (nonatomic, strong) NSString *filePath;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *backButton = [[UIButton alloc] init];
    backButton.bounds = CGRectMake(0, 0, 40, 40);
    backButton.center = CGPointMake(kScreenWidth/2, kScreenHeight/2 - 50);
    [backButton setTitle:@"录制" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [backButton addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UIButton *playButton = [[UIButton alloc] init];
    playButton.bounds = CGRectMake(0, 0, 40, 40);
    playButton.center = CGPointMake(kScreenWidth/2, kScreenHeight/2 + 50);
    [playButton setTitle:@"播放" forState:UIControlStateNormal];
    [playButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    playButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];
}

- (void)record:(UIButton *)sender
{
    WQVideoPreviewViewController *previewVC = [[WQVideoPreviewViewController alloc] init];
    previewVC.videoRecorderMaxTime = 3.0;
    previewVC.shootWay = @"tap";
    [self presentViewController:previewVC animated:YES completion:nil];
    previewVC.exportComplete = ^(NSString * _Nonnull path, NSString * _Nonnull fileName) {
        // 录制视频成功回调：path/name
        self.filePath = path;
    };
}

- (void)play:(UIButton *)sender
{
    WQVideoPlayViewController *playVC = [[WQVideoPlayViewController alloc] init];
    playVC.isOnlyPlay = YES;
    
    if ([self.filePath containsString:@"http"] || [self.filePath containsString:@"https"]) {
        playVC.fileUrl = [NSURL URLWithString:self.filePath];
    } else {
        playVC.fileUrl = [NSURL fileURLWithPath:self.filePath];
    }
    [self presentViewController:playVC animated:YES completion:nil];
}

@end
